/*
    This file is part of tPortal.

    tPortal is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    tPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with tPortal.  If not, see <http://www.gnu.org/licenses/>.

    The original code was written by Sebastien Bettinger <sebastien.bettinger@gmail.com>

 */

package net.tinyportal.portlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import net.tinyportal.persistence.entity.Layout;
import net.tinyportal.service.LayoutService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ManagePage extends GenericPortlet {

	@Autowired
	LayoutService layoutService;

	private static final Logger logger = Logger.getLogger(ManagePage.class);

	public void doView(RenderRequest request,RenderResponse response) 
			throws PortletException,IOException {
		List<Layout> layouts = layoutService.getAllLayout();
		request.setAttribute("layouts", layouts);
		PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/portlets/managePage/view.jsp");
		dispatcher.include(request, response);
	}

	public void doEdit(RenderRequest request,RenderResponse response) 
			throws PortletException,IOException {
		List<Layout> layouts = layoutService.getAllLayout();
		request.setAttribute("layouts", layouts);
		PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/portlets/managePage/edit.jsp");
		dispatcher.include(request, response);
	}

	public void processAction (ActionRequest request, ActionResponse response) 
			throws PortletException, java.io.IOException {
		
		if (request.getPortletMode().equals(PortletMode.EDIT)) {
			System.out.println(request.getParameterMap());
			String layout = request.getParameter("layout");
			layoutService.addSimpleLayout(layout);
			

		}
	}
}
